#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

typedef struct LNode *PtrToNode;
struct LNode
{
    double Data;
    PtrToNode Next;
};
typedef PtrToNode Position; //结点
typedef PtrToNode List;     //链表结构体

List insertElem(List L, double X, int i);

int main()
{
    List L = NULL;
    double num;

    for (int i = 1; i < 6; i++)
    {
        scanf("%lf", &num);
        L = insertElem(L, num, i);
    }

    for (List tmp = L; tmp != NULL; tmp = tmp->Next)
        printf("%lf ", tmp->Data);
    printf("\n");

    printf("%lf", L->Data);

    return 0;
}

List insertElem(List L, double X, int i)
{
    Position tmp, pre;

    tmp = (Position)malloc(sizeof(struct LNode));
    tmp->Data = X;

    /*
    如果L为空表，则插入第一个结点时，第一个结点的指向为空
    */

    if(i == 1)
    {
        tmp->Next = L;
        printf("Initial list success !\n");
        return tmp;
    } else
    {
        int cnt = 1;    //设置结点位序值
        pre = L;        //pre指向L的第1个结点
        while (pre && cnt < i - 1)
        {
            pre = pre->Next;    //只要pre!=NULL，pre不断指向下一个结点
            cnt++;              //并且位序值加1
        }
        if(pre == NULL || cnt != i - 1) //等价条件？
        {
            printf("Invalid Insert Position !\n");  
            free(tmp);
            return NULL;
        } else
        {
            tmp->Next = pre->Next;  //先用新结点取代原结点
            pre->Next = tmp;        //原结点指向新节点，完成插入
            printf("Insert Success !\n");
            return L;
        }
    }
}